package com.hope.androidproject.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hope.androidproject.R;
import com.hope.androidproject.bean.News;

import java.util.List;

/**
 * Created by XLC on 2017/7/21.
 */

public class NewsListAdapter extends BaseAdapter {

    private List<News> data;
    private Context context;

    public NewsListAdapter(List<News> data, Context context) {
        this.data = data;
        this.context = context;
    }
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if(convertView == null)
        {
            convertView = View.inflate(context, R.layout.news_item,null);
            viewHolder = new ViewHolder();
            viewHolder.newsTitle = (TextView) convertView.findViewById(R.id.news_title);
            viewHolder.newsTime = (TextView) convertView.findViewById(R.id.news_time);
            viewHolder.newsInfo = (TextView) convertView.findViewById(R.id.news_info);
            viewHolder.newsImage = (ImageView) convertView.findViewById(R.id.news_img);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //设置数据
        News news = data.get(position);
        viewHolder.newsTitle.setText(news.getNewsTiele());
        viewHolder.newsInfo.setText(news.getNewsInfo());
        viewHolder.newsTime.setText(news.getNewsTime());
        //使用Glide款框架加载图片
        Glide.with(context).load(news.getNewsImagePath()).into(viewHolder.newsImage);
        return convertView;
    }

    static class ViewHolder{
        TextView newsTitle,newsTime,newsInfo;
        ImageView newsImage;
    }
}
